﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using LeadMaster.Models;
using LeadMaster.DAL;

namespace LeadMaster.Controllers
{
    public class ClientUsersController : Controller
    {
        private DunsmoreContext db = new DunsmoreContext();

        //
        // GET: /ClientUsers/

        public ActionResult Index()
        {
          try
          {
            return View(db.ClientUsers.ToList());
          }
          catch (Exception e)
          {
            ViewBag.ErrorMessage = "Client Users Index " + e.LineNumber().ToString() + " : " + e.Message + " - " + e.InnerException;
            return View("Errormessages");
          }
            
        }

        //
        // GET: /ClientUsers/Details/5

        public ActionResult Details(int id = 0)
        {
            ClientUsers clientusers = db.ClientUsers.Find(id);
            if (clientusers == null)
            {
                return HttpNotFound();
            }
            return View(clientusers);
        }

        //
        // GET: /ClientUsers/Create

        public ActionResult Create()
        {
            try
            {
                ViewBag.UserID = new SelectList(db.Users.OrderBy(m => m.UserName), "UserID", "UserName");
                ViewBag.CompanyID = db.Companies
                        .OrderBy(m => m.Company)
                        .ToList()
                           .Select(c => new SelectListItem
                           {
                               Value = c.CompanyID.ToString(),
                               Text = c.Company
                           });
            }
            catch (Exception e)
            {
                ViewBag.ErrorMessage = "Client Users Index " + e.LineNumber().ToString() + " : " + e.Message + " - " + e.InnerException;
                return View("Errormessages");
            }

            return View();
        }

        //
        // POST: /ClientUsers/Create

        [HttpPost]
        public ActionResult Create(ClientUsers clientusers)
        {
            if (ModelState.IsValid)
            {
                db.ClientUsers.Add(clientusers);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(clientusers);
        }

        //
        // GET: /ClientUsers/Edit/5

        public ActionResult Edit(int id = 0)
        {
            ClientUsers clientusers = db.ClientUsers.Find(id);
            if (clientusers == null)
            {
                return HttpNotFound();
            }

            try
            {
                ViewBag.UserID = new SelectList(db.Users.OrderBy(m => m.UserName), "UserID", "UserName");
                ViewBag.CompanyID = db.Companies
                        .OrderBy(m => m.Company)
                        .ToList()
                           .Select(c => new SelectListItem
                           {
                               Value = c.CompanyID.ToString(),
                               Text = c.Company
                           });
            }
            catch (Exception e)
            {
                ViewBag.ErrorMessage = "Client Users Index " + e.LineNumber().ToString() + " : " + e.Message + " - " + e.InnerException;
                return View("Errormessages");
            }

            return View(clientusers);
        }

        //
        // POST: /ClientUsers/Edit/5

        [HttpPost]
        public ActionResult Edit(ClientUsers clientusers)
        {
            if (ModelState.IsValid)
            {
                db.Entry(clientusers).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(clientusers);
        }

        //
        // GET: /ClientUsers/Delete/5

        public ActionResult Delete(int id = 0)
        {
            ClientUsers clientusers = db.ClientUsers.Find(id);
            if (clientusers == null)
            {
                return HttpNotFound();
            }
            return View(clientusers);
        }

        //
        // POST: /ClientUsers/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            ClientUsers clientusers = db.ClientUsers.Find(id);
            db.ClientUsers.Remove(clientusers);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}